package view;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import server.FirewallException;
import server.NotExpertException;
import server.Protocol;
import server.ReadWriteException;

import server.FirewallSession;


/**
 * Summary description for AddRegexp
 *
 */
public class RegExpFrame extends JFrame
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 6874649053106615548L;

	/**
	 * server controller
	 */
	protected FirewallSession UI;
	
	public static RegExpFrame currentRegExpFrame = null;
	
	// Variables declaration
	private JLabel lblProtocol;
	private JLabel lblRegexp;
	private JLabel lblRegexpIn;
	private JLabel lblRegexpOut;
	private JTextField txtProtocol;
	private JTextField txtRegexpIn;
	private JTextField txtRegexpOut;

	private JButton cmdFinish;
	private JButton cmdCancel;
	private JButton cmdHelp;
	private JPanel contentPane;
	// End of variables declaration


	private RegExpFrame(FirewallSession UI)
	{
		super();
		this.UI = UI;
		initializeComponent();
		this.setVisible(true);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always regenerated
	 * by the Windows Form Designer. Otherwise, retrieving design might not work properly.
	 * Tip: If you must revise this method, please backup this GUI file for JFrameBuilder
	 * to retrieve your design properly in future, before revising this method.
	 */
	private void initializeComponent()
	{
		lblProtocol = new JLabel();
		lblRegexp = new JLabel();
		lblRegexpIn = new JLabel();
		lblRegexpOut = new JLabel();
		txtProtocol = new JTextField();
		txtRegexpIn =  new JTextField();
		txtRegexpOut =  new JTextField();
		cmdFinish = new JButton();
		cmdCancel = new JButton();
		cmdHelp = new JButton();
		contentPane = (JPanel)this.getContentPane();

		//
		// lblProtocol
		//
		lblProtocol.setText("Protocol:");
		//
		// lblRegexp
		//
		lblRegexp.setText("Regular Expression:");
		lblRegexpIn.setText("IN:");
		lblRegexpOut.setText("OUT:");

		//
		// cmdFinish
		//
		cmdFinish.setText("Finish");
		cmdFinish.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				cmdFinish_actionPerformed(e);
			}

		});
		//
		// cmdCancel
		//
		cmdCancel.setText("Cancel");
		cmdCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				cmdCancel_actionPerformed(e);
			}

		});
		//
		// cmdHelp
		//
		cmdHelp.setText("Help");
		cmdHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				cmdHelp_actionPerformed(e);
			}

		});
		//
		// contentPane
		//
		contentPane.setLayout(null);
		addComponent(contentPane, lblProtocol, 21,17,60,18);
		addComponent(contentPane, lblRegexp, 24,76,106,18);
		addComponent(contentPane, txtProtocol, 21,40,100,22);
		addComponent(contentPane, txtRegexpIn, 60,100,449,22);
		addComponent(contentPane, lblRegexpIn, 24,100,30,18);
		
		addComponent(contentPane, txtRegexpOut, 60,130,449,22);
		addComponent(contentPane, lblRegexpOut, 24,130,30,18);
		
		addComponent(contentPane, cmdFinish, 36,183,83,29);
		addComponent(contentPane, cmdCancel, 138,183,83,28);
		addComponent(contentPane, cmdHelp, 239,183,83,28);
		//
		// AddRegexp
		//
		this.setTitle("Adding Regular Expression");
		this.setLocation(new Point(100, 100));
		this.setSize(new Dimension(526, 247));
	}

	/** Add Component Without a Layout Manager (Absolute Positioning) */
	private void addComponent(Container container,Component c,int x,int y,int width,int height)
	{
		c.setBounds(x,y,width,height);
		container.add(c);
	}

	private void cmdFinish_actionPerformed(ActionEvent e)
	{
		String protocol_name = txtProtocol.getText();
		String regExpIn = txtRegexpIn.getText();
		String regExpOut = txtRegexpOut.getText();
		
		Protocol protocol = new Protocol (protocol_name, regExpOut ,regExpIn);
		try {
			UI.addProtocol(protocol);
		} catch (RemoteException e1) {
			ErrorFrame.AddError("Failed to add new protocol definition:" + e1.getMessage(), new JFrame());
		} catch (FirewallException e1) {
			ErrorFrame.AddError("Failed to add new protocol definition:" + e1.getMessage(), new JFrame());
		} catch (ReadWriteException e1) {
			ErrorFrame.AddError("Failed to add new protocol definition:" + e1.getMessage(), new JFrame());
		} catch (NotExpertException e1) {
			ErrorFrame.AddError("Failed to add new protocol definition:" + e1.getMessage(), new JFrame());
		}
		dispose();
	}

	private void cmdCancel_actionPerformed(ActionEvent e)
	{
		dispose();
	}

	private void cmdHelp_actionPerformed(ActionEvent e)
	{
		HelpFrame.currentHelpFrame.ShowDesc(Globals.REG_EXP);
	}

	public static RegExpFrame getInstance(FirewallSession ui) 
	{
		if(currentRegExpFrame == null)
		{
			currentRegExpFrame = new RegExpFrame(ui);
		}

		return currentRegExpFrame;
	}
}
